package mail;

import semantic.*;

import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.text.SimpleDateFormat;

public class Mail {
    private MimeMessage mimeMessage = null;
    private String from;
    private String date;
    private StringBuffer content = new StringBuffer();
    private String subject;

    // The format of the date to be displayed.
    private final String dateFormat = "yy-MM-dd HH:mm";
    private static Semantic semantic;

    public Mail(MimeMessage mimeMessage) throws Exception {
        this.mimeMessage = mimeMessage;
        Part part = mimeMessage;
        analyseMail(part);

        subject = MimeUtility.decodeText(mimeMessage.getSubject());

        InternetAddress address[] = (InternetAddress[]) mimeMessage.getFrom();
        from = address[0].getPersonal() + "<" + address[0].getAddress() + ">";

        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        date = format.format(mimeMessage.getSentDate());
    }

    /**
     * Get the sender's address and name.
     *
     * @return "sender's name<sender's address>"
     */
    public String getFrom() throws Exception {
        return from;
    }

    /**
     * Get the subject of the mail.
     */
    public String getSubject() throws Exception {
        return subject;
    }

    /**
     * Get the sent date.
     */
    public String getSentDate() throws Exception {
        return date;
    }

    /**
     * Get the content.
     */
    public String getContent() {
        return content.toString();
    }

    /**
     * Analyse the mail.
     */
    public void analyseMail(Part part) throws Exception {
        if (part.isMimeType("multipart/*")) {
            Multipart p = (Multipart) part.getContent();
            int count = p.getCount();
            if (count > 1) {
                count = 1;
            }
            for (int i = 0; i < count; ++i) {
                BodyPart bodyPart = p.getBodyPart(i);
                analyseMail(bodyPart);
            }
        } else if (part.isMimeType("text/*")) {
            content.append(part.getContent());
        }
    }

    public String getResponse() {
        String response;
        int i = content.indexOf("----");
        if (i >= 0) {
            response = content.substring(0, i);
        } else {
            response = String.valueOf(content);
        }
        return response;
    }

    /**
     * Display the mail in detail.
     * @param choice true represents show content only,
     *               false represents append semantic analysis result.
     */
    public void display(boolean choice) throws Exception {
        System.out.println("---------------------------------------------");
        System.out.println("Subject: " + getSubject());
        System.out.println("From: " + getFrom());
        System.out.println("Date: " + getSentDate());
        System.out.println("-------------------CONTENT-------------------");
        if (choice == true) {
            System.out.println(getContent());
        } else {
            String text = getResponse();
            System.out.println(text);
            System.out.println("------------Semantic Analysis------------");
            if (semantic == null) {
                semantic = new Semantic();
            } else {
                System.out.println("Prediction : "
                        +semantic.getPrediction(new Sentence(text)));
            }
        }
        System.out.println("---------------------------------------------");
    }
}
